﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Structures;

namespace X3DXamlXna
{
    public interface IDrawableModel
    {
        BasicEffect effect { get; }
        List<X3dModel> models { get; set; }
        bool ready { get; set; }
    }

    class DrawableComponent : Microsoft.Xna.Framework.DrawableGameComponent, IDrawableModel
    {

        public BasicEffect effect { get; private set; }
        public bool ready { get; set; }

        public List<X3dModel> models { get; set; }
        private SpriteBatch sb;

        public DrawableComponent(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IDrawableModel), this);
        }
        public override void Initialize()
        {
            models = new List<X3dModel>();
            base.Initialize();
            effect = new BasicEffect(Game.GraphicsDevice);
            effect.LightingEnabled = true;
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();
            base.Initialize();
            ready = true;
        }
        protected override void LoadContent()
        {
            sb = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            ready = false;
            var camera = Game.Services.GetService(typeof(ICameraService)) as ICameraService;

            effect.View = camera.CurrentCamera.View;
            effect.Projection = camera.CurrentCamera.Projection;
            effect.CurrentTechnique.Passes[0].Apply();


            foreach (var elem in models)
            {
                //if (Keyboard.GetState().IsKeyDown(Keys.F1))
                //    elem.DrawBSphereModel(camera);
                elem.DrawModel(GraphicsDevice, effect);

            }
            ready = true;
            base.Draw(gameTime);
        }
    }
}

